package gameuserlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/common/key/gamekey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"navi-mumbai/third/game/wz"

	"navi-mumbai/app/rpc/game/internal/svc"
	"navi-mumbai/app/rpc/game/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GuestLoginGameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGuestLoginGameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuestLoginGameLogic {
	return &GuestLoginGameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 游客登陆游戏
func (l *GuestLoginGameLogic) GuestLoginGame(in *pb.GuestLoginGameReq) (*pb.GuestLoginGameResp, error) {
	defer utils.RecoverPanic()

	switch in.ProviderId {
	case gamekey.ProviderIdY9Y:
		break
	default:
		return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "ProviderId error")
	}

	var wzReq wz.GuestAuthReq
	wzReq.Currency = walletkey.GetMoneyNameByMoneyType(walletkey.UsingCurrencyType)
	wzReq.Lang = "en"
	var wzRsp *wz.GuestAuthResp
	var err error
	wzRsp, err = wz.Hc.GuestAuth(&wzReq)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}
	var rsp pb.GuestLoginGameResp
	rsp.Nickname = wzRsp.Data.Username
	rsp.GToken = wzRsp.Data.Token
	rsp.UserId = wzRsp.Data.UserId
	return &rsp, nil
}
